# API Test Script for Book Management System

$baseUrl = "http://localhost:3001/api/books"

Write-Host "========================================"
Write-Host "  Book Management API Tests"
Write-Host "========================================"
Write-Host ""

# Test 1: GET all books
Write-Host "[Test 1] GET /api/books - Get All Books"
Write-Host "----------------------------------------"
try {
    $response = Invoke-RestMethod -Uri $baseUrl -Method Get
    Write-Host "PASS - Found $($response.data.books.Count) books"
    Write-Host "Pagination: Page $($response.data.pagination.currentPage) of $($response.data.pagination.totalPages)"
    Write-Host ""
} catch {
    Write-Host "FAIL: $_"
}

# Test 2: POST new book
Write-Host "[Test 2] POST /api/books - Create New Book"
Write-Host "----------------------------------------"
$newBook = @{
    title = "AI Programming Guide 2024"
    author = "Test Author"
    publishDate = "2024-01-15"
    price = 128.50
    isbn = "9787302999999"
    publisher = "Tsinghua University Press"
    category = "AI"
    description = "Learn AI-assisted programming"
    stock = 50
} | ConvertTo-Json

try {
    $response = Invoke-RestMethod -Uri $baseUrl -Method Post -Body $newBook -ContentType "application/json"
    $bookId = $response.data._id
    Write-Host "PASS - Created book ID: $bookId"
    Write-Host "Title: $($response.data.title)"
    Write-Host ""
    $global:testBookId = $bookId
} catch {
    Write-Host "FAIL: $_"
}

Start-Sleep -Seconds 1

# Test 3: GET single book
Write-Host "[Test 3] GET /api/books/:id - Get Single Book"
Write-Host "----------------------------------------"
if ($global:testBookId) {
    try {
        $response = Invoke-RestMethod -Uri "$baseUrl/$global:testBookId" -Method Get
        Write-Host "PASS - Book ID: $($response.data._id)"
        Write-Host "Title: $($response.data.title)"
        Write-Host "Author: $($response.data.author)"
        Write-Host "Price: $($response.data.price)"
        Write-Host ""
    } catch {
        Write-Host "FAIL: $_"
    }
} else {
    Write-Host "SKIP - No book ID available"
}

# Test 4: DELETE book
Write-Host "[Test 4] DELETE /api/books/:id - Delete Book"
Write-Host "----------------------------------------"
if ($global:testBookId) {
    try {
        $response = Invoke-RestMethod -Uri "$baseUrl/$global:testBookId" -Method Delete
        Write-Host "PASS - Deleted: $($response.data.title)"
        Write-Host "ID: $($response.data.id)"
        Write-Host ""
    } catch {
        Write-Host "FAIL: $_"
    }
} else {
    Write-Host "SKIP - No book ID available"
}

# Verify deletion
Write-Host "[Verify] GET /api/books - Verify Deletion"
Write-Host "----------------------------------------"
try {
    $response = Invoke-RestMethod -Uri $baseUrl -Method Get
    Write-Host "PASS - Total books now: $($response.data.pagination.totalItems)"
    Write-Host ""
} catch {
    Write-Host "FAIL: $_"
}

Write-Host "========================================"
Write-Host "  All Tests Completed!"
Write-Host "========================================"
Write-Host ""

